home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wdj0797.zip / TOMLINSN.ZIP / SECURITY.C next >
C/C++ Source or Header  |  1997-04-28  |  7KB  |  207 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "security.h"
  5.  
  6. //------------------------------------------------------------------
  7. VOID DumpWinStaSD(VOID)
  8. {
  9.     SECURITY_INFORMATION Sec =  OWNER_SECURITY_INFORMATION | 
  10.                                 GROUP_SECURITY_INFORMATION | 
  11.                                 DACL_SECURITY_INFORMATION;
  12.     DWORD   Size = 0;
  13.     HWINSTA hWinSta = NULL;
  14.     char    szName[MAX_PATH];
  15.     PSECURITY_DESCRIPTOR pSD = malloc(SD_BUF);
  16.  
  17.     hWinSta = GetProcessWindowStation();
  18.     if (hWinSta) {
  19.         if (GetUserObjectInformation(hWinSta, UOI_NAME, szName, 
  20.                                    MAX_PATH * sizeof(char), &Size)) {
  21.             Dbg1("\nDUMP WINDOW STATION SD: %s\n", szName); 
  22.         }     
  23.         if(GetUserObjectSecurity(hWinSta, &Sec, pSD, SD_BUF, &Size)){
  24.             DumpSD(pSD, WDJSRVC_WINSTA);
  25.         }
  26.         CloseHandle(hWinSta); 
  27.     }
  28.     if (pSD) free(pSD);
  29.  
  30. } // DumpWinStaSD 
  31.  
  32. //------------------------------------------------------------------
  33. VOID DumpDesktopSD(VOID)
  34. {
  35.     SECURITY_INFORMATION Sec =  OWNER_SECURITY_INFORMATION | 
  36.                                 GROUP_SECURITY_INFORMATION | 
  37.                                 DACL_SECURITY_INFORMATION;
  38.     DWORD   Size = 0;
  39.     HDESK   hDesktop = NULL;
  40.     char    szName[MAX_PATH];
  41.     PSECURITY_DESCRIPTOR pSD = malloc(SD_BUF);
  42.  
  43.     hDesktop = GetThreadDesktop(GetCurrentThreadId());
  44.     if (hDesktop) {    
  45.         if (GetUserObjectInformation(hDesktop, UOI_NAME, szName, 
  46.                                  MAX_PATH * sizeof(char), &Size)) {
  47.             Dbg1("\nDUMP DESKTOP SD: %s\n", szName); 
  48.         }
  49.         if(GetUserObjectSecurity(hDesktop, &Sec,pSD,SD_BUF,&Size)){
  50.             DumpSD(pSD, WDJSRVC_DESKTOP);
  51.         } 
  52.         CloseHandle(hDesktop); 
  53.     }
  54.     if (pSD) free(pSD);
  55.  
  56. } // DumpDesktopSD 
  57.  
  58. //------------------------------------------------------------------
  59. VOID DumpSD(PSECURITY_DESCRIPTOR pSD, DWORD ObjectType)
  60. {
  61.     PSID sid = NULL;
  62.     PACL dacl = NULL;
  63.     BOOL bDefault, bPresent;
  64.  
  65.     if (!IsValidSecurityDescriptor(pSD)) {
  66.         return;
  67.     }
  68.  
  69.     if (GetSecurityDescriptorOwner(pSD, &sid, &bDefault)) {
  70.         Dbg("-> Owner: ");
  71.         if (bDefault) {
  72.            Dbg("(SE_OWNER_DEFAULTED) ");
  73.         }
  74.         if (sid) { 
  75.             DumpSid(sid, 0);
  76.         }
  77.     }
  78.  
  79.      if (GetSecurityDescriptorGroup(pSD, &sid, &bDefault)) {
  80.         Dbg("-> Primary Group: ");
  81.         if (bDefault) {
  82.            Dbg("(SE_OWNER_DEFAULTED) ");
  83.         }
  84.         if (sid) { 
  85.             DumpSid(sid, 0);
  86.         } 
  87.     }
  88.  
  89.     if (GetSecurityDescriptorDacl(pSD, &bPresent, &dacl, &bDefault)) {
  90.  
  91.         Dbg("-> Discretionary Access Control List: \n");
  92.         if (bPresent) {
  93.             if (bDefault) {
  94.                 Dbg("   SE_OWNER_DEFAULTED flag is set\n");
  95.             }
  96.             DumpAcl(dacl, ObjectType);
  97.         }
  98.     }
  99.     return;
  100.  
  101. } // DumpSD
  102.  
  103. VOID DumpSid(PSID Sid, DWORD Attributes)
  104. {
  105.     SID_NAME_USE Use;
  106.     char szAccount[MAX_PATH], szDomain[MAX_PATH];
  107.     ULONG size1 = MAX_PATH, size2 = MAX_PATH;
  108.  
  109.     if (LookupAccountSid(NULL, Sid, szAccount, &size1, szDomain,
  110.                          &size2, &Use)) {        
  111.         Dbg1("%s\\", szDomain);
  112.         Dbg1("%s, ", szAccount);
  113.         Dbg1("Attributes: %x\n", Attributes);
  114.     } else {
  115.         Dbg("Unknown\n");
  116.     }
  117.         
  118. } // DumpSid
  119.  
  120. VOID DumpAcl(PACL Acl, DWORD ObjectType)
  121. {
  122.     PACCESS_ALLOWED_ACE ace;
  123.     ULONG i;
  124.  
  125.     //
  126.     // Dump info for each ace in the acl
  127.     //
  128.     for (i=0; i < Acl->AceCount; i++) {
  129.         if (GetAce(Acl, i, (LPVOID *)&ace)) {
  130.             if (ace->Header.AceType == ACCESS_ALLOWED_ACE_TYPE) {
  131.                 Dbg("   Access ALLOWED: ");
  132.             }else if(ace->Header.AceType == ACCESS_DENIED_ACE_TYPE){
  133.                 Dbg("   Access DENIED: ");
  134.             } else {
  135.                 Dbg("   Audit ace: ");
  136.             }
  137.             DumpSid((PSID)&(ace->SidStart), 0);
  138.             DumpAccessMask(ace->Mask, ObjectType);
  139.         }
  140.     }
  141.  
  142.     return;
  143.  
  144. } // DumpAcl
  145.  
  146. VOID DumpAccessMask(ACCESS_MASK Mask, DWORD ObjectType)
  147. {
  148.     DWORD Specific = 0;
  149.  
  150.     Dbg("   Access: ");
  151.     
  152.     // Bits 16 - 23 (16-20 currently defined): Standard rights
  153.     if (Mask & DELETE) Dbg("DELETE ");
  154.     if (Mask & READ_CONTROL) Dbg("READ_CONTROL ");
  155.     if (Mask & WRITE_DAC) Dbg("WRITE_DAC ");
  156.     if (Mask & WRITE_OWNER) Dbg("WRITE_OWNER ");
  157.     if (Mask & SYNCHRONIZE) Dbg("SYNCHRONIZE ");
  158.  
  159.     // Bit 24: Access system security
  160.     if (Mask & ACCESS_SYSTEM_SECURITY) Dbg("ACCESS_SYSTEM_SECURITY ");
  161.  
  162.     // Bit 25: Maximum allowed
  163.     if (Mask & MAXIMUM_ALLOWED) Dbg("MAXIMUM_ALLOWED ");
  164.  
  165.     // Bits 26 - 27: Reserved
  166.  
  167.     // Bits 28 - 31: Generic Rights
  168.     if (Mask & GENERIC_ALL) Dbg("ENERIC_ALL ");
  169.     if (Mask & GENERIC_EXECUTE) Dbg(",GENERIC_EXECUTE ");
  170.     if (Mask & GENERIC_WRITE) Dbg("GENERIC_WRITE ");
  171.     if (Mask & GENERIC_READ) Dbg("GENERIC_READ ");
  172.  
  173.     // Bits 0 - 15: Specific Rights
  174.     Specific = Mask & SPECIFIC_RIGHTS_ALL;
  175.     Dbg1("\n   Specific(%x)", Specific);
  176.  
  177.     if (ObjectType == WDJSRVC_WINSTA) {
  178.         if(Specific & WINSTA_ENUMDESKTOPS) Dbg(",ENUMDESKTOPS");
  179.         if(Specific & WINSTA_READATTRIBUTES) Dbg(",READATTRIBUTES");
  180.         if(Specific & WINSTA_ACCESSCLIPBOARD)
  181.             Dbg(",ACCESSCLIPBOARD");
  182.         if(Specific & WINSTA_CREATEDESKTOP) Dbg(",CREATEDESKTOP");
  183.         if(Specific & WINSTA_WRITEATTRIBUTES)
  184.             Dbg(",WRITEATTRIBUTES");
  185.         if(Specific & WINSTA_ACCESSGLOBALATOMS)
  186.             Dbg(",ACCESSGLOBALATOMS");
  187.         if(Specific & WINSTA_EXITWINDOWS) Dbg(",EXITWINDOWS");
  188.         if(Specific & WINSTA_ENUMERATE) Dbg(",ENUMERATE");
  189.         if(Specific & WINSTA_READSCREEN) Dbg(",READSCREEN");
  190.     }
  191.  
  192.     if (ObjectType == WDJSRVC_DESKTOP) {
  193.         if(Specific & DESKTOP_READOBJECTS) Dbg(",READOBJECTS");
  194.         if(Specific & DESKTOP_CREATEWINDOW) Dbg(",CREATEWINDOW");
  195.         if(Specific & DESKTOP_CREATEMENU) Dbg(",CREATEMENU");
  196.         if(Specific & DESKTOP_HOOKCONTROL) Dbg(",HOOKCONTROL");
  197.         if(Specific & DESKTOP_JOURNALRECORD) Dbg(",JOURNALRECORD");
  198.         if(Specific & DESKTOP_JOURNALPLAYBACK)
  199.             Dbg(",JOURNALPLAYBACK");
  200.         if(Specific & DESKTOP_ENUMERATE) Dbg(",ENUMERATE");
  201.         if(Specific & DESKTOP_WRITEOBJECTS) Dbg(",WRITEOBJECTS");
  202.         if(Specific & DESKTOP_SWITCHDESKTOP) Dbg(",SWITCHDESKTOP");
  203.     }
  204.     Dbg("\n");
  205.  
  206. } // DumpAccessMask
  207.